//
// Created by 爪子 on 2025/1/19.
//
#include <string.h>
#include "stdio.h"
#include "stdlib.h"

//定义书籍结构体
struct book {
    int bid;//书籍编号
    char bname[100];//书名
    char author[30];//作者
    float price;//价格
    char publish[100];//出版社
    int state;//书籍状态 1代表已借出 0 未借出
};

//用户结构体
struct user {
    char uname[20];//用户名 admin 代表管理员 rose代表普通用户
    char password[30];//密码
    int bid;//编号-1代表管理员 0 代表未借书 不等于0或-1 已借书籍编号
};

//初始化两个用户admin管理员 rose普通用户
struct user users[2] = {{"admin", "123456", -1},
                        {"rose",  "111111", 0}};
//初始化书籍信息
struct book books[100] = {{1001, "c语言程序设计",  "唐浩强", 45.6, "清华大学出版社",   0},
                          {1002, "数据结构",       "闫蔚敏", 90,   "辽宁大学出版社",   0},
                          {1003, "疯狂的java讲义", "程杰",   79,   "家里蹲大学出版社", 0}};

int last = 3;//书籍结构体数组中有效元素的个数 添加书籍时1ast++ 删除书籍last--
struct user *user1;//记录当前登录的用户
struct user *login();//登录函数 登录成功返回当前用户 失败返回NULL
void initMenu();//初始化界面
void showAdminMenu();//管理员菜单
void showuserMenu();//普通用户菜单
void addBook();//管理员添加图书信息
void delBook();//管理员删除图书信息
void updateBook();//管理员修改图书价格

//管理员査看所有书籍 全部展示 已借出 和 未借出
void showBook();//普通用户査看所有书籍 只展示未借出的书籍信息
void borrowBook();//普通用户借书
void returnBook();//普通用户还书

int main() {
    initMenu();
    return 0;
}

void initMenu() {
    while (1) {
        printf("-----------------------------\n");
        printf("|       欢迎来到图书馆　　　|\n");
        printf("|                     　　　|\n");
        printf("-----------------------------\n");
        printf("-------1.登录　　2.退出------\n");
        


        printf("请选择：");

        int choice;
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                user1 = login();
                if (user1 != NULL) {
                    if (strcmp(user1->uname, "admin") == 0) {
                        user1->bid = -1;
                        showAdminMenu();
                    } else {
                        user1->bid = 0;
                        showuserMenu();
                    }
                } else {
                    printf("用户名或密码错误 请重新输入\n");
                    continue;
                }
                break;
            case 2:
                exit(0);
            default:
                printf("输入错误 请重新输入\n");
                break;
        }
    }
}

struct user *login() {
    struct user *user2 = (struct user *) malloc(sizeof(struct user));

    printf("请输入用户名：");
    scanf("%s", user2->uname);
    puts("\n");

    printf("请输入密码：");
    scanf("%s", user2->password);
    puts("\n");

    for (int i = 0; i < 2; ++i) {
        if (strcmp(user2->uname, users[i].uname) == 0 && strcmp(user2->password, users[i].password) == 0) {
            return user2;
        }
    }
    free(user2);
    return NULL;
}

void showAdminMenu() {
    printf("\n************************************************\n");
    printf("**************欢迎来到Ｔｉｎａ书店****************\n");
    printf("1.添加书籍 2.删除书籍 3.修改书籍 4.查看书籍 5.退出\n");
    printf("请选择：");
    while (1) {
        int choice;
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                addBook();
                break;
            case 2:
                delBook();
                break;
            case 3:
                updateBook();
                break;
            case 4:
                showBook();
                break;
            case 5:
                free(user1);
                initMenu();
                return;
            default:
                printf("输入错误 请重新输入\n");
                break;
        }
    }
}

void showuserMenu() {
    printf("\n*******************************************\n");
    printf("*********欢迎来到Ｔｉｎａ书店****************\n");
    printf("-----1.查看书籍---2.借书---3.还书----４.退出-\n");
    printf("请选择：");
    while (1) {
        int choice;
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                showBook();
                break;
            case 2:
                borrowBook();
                break;
            case 3:
                returnBook();
                break;
            case 4:
                free(user1);
                initMenu();
                return;
            default:
                printf("输入错误 请重新输入\n");
                break;
        }
    }
}

void borrowBook() {
    int BookId;
    int i;
    if(user1->bid != 0){
    	printf("您已经借了一本");
    	showuserMenu();
    	return;
    }
    for (i = 0; i < last; i++) {
            if (books[i].state == 0) {
                printf("编号：%d 书名：%s 作者：%s 价格：%0.2f 出版社：%s 状态：%d\n", books[i].bid, books[i].bname, books[i].author, books[i].price, books[i].publish, books[i].state);
            }
     }
    printf("要借的书籍的编号:");
    scanf("%d", &BookId);
    for (i = 0; i < last; i++) {
        if (BookId == books[i].bid && books[i].state == 0) {
            books[i].
                    state = 1;
            user1->
                    bid = BookId;
            printf("借出成功!\n");
            showuserMenu();
            return;
        }else if(BookId == books[i].bid && books[i].state != 0){
        	printf("书籍已被借出\n");
        	showuserMenu();
        	return;
        }
    }
    printf("查无此书\n");
    showuserMenu();
}

void returnBook() {
    if (user1->bid == 0) {
        printf("用户未借出书籍\n");
        return;
    }

    int i;
    for (i = 0; i < last; i++) {
        if (books[i].bid == user1->bid) {
            books[i].state = 0;
            printf("书籍编号%d还书成功!\n", user1->bid);
            user1->bid = 0;
            showuserMenu();
            return;
        }
    }
    printf("此书未被借出\n");
    showuserMenu();
}

void addBook() {
    printf("请输入书籍编号:");

    int tmp;
    scanf("%d", &tmp);

    puts("\0");

    int i = 0;
    for (i = 0; i < last; i++) {
        if (tmp == books[i].bid) {
            printf("编号已存在\n");
            showAdminMenu();
            return;
        }
    }

    books[last].bid = tmp;

    printf("请输入书名:");
    scanf("%s", books[last].bname);
    puts("\0");

    printf("请输入书籍作者:");
    scanf("%s", books[last].author);
    puts("\0");

    printf("请输入书籍价格:");
    scanf("%f", &books[last].price);
    puts("\0");

    printf("请输入书籍出版社:");
    scanf("%s", books[last].publish);
    puts("\0");

    books[i].state = 0;
    last++;
    printf("添加图书成功\n");
    
	showAdminMenu();
}

void delBook() {
    struct book b;
    printf("请输入要删除的书籍编号:");
    scanf("%d", &b.bid);
    puts("\0");
    int temp = 0, i = 0, j = 0;
    for (i = 0; i < last; i++) {
        if (b.bid == books[i].bid) {
            temp = i;
            for (j = temp; j < last; j++) {
                books[j] = books[j + 1];
            }
            printf("删除成功\n");
    		last--;
    		showAdminMenu();
    		return;
        }
    }
    printf("查无此书\n");
    showAdminMenu();
}

void updateBook(){
	printf("请输入需要修改价格的书籍编号");
	int tmp = 0;
	scanf("%d", &tmp);
	for(int i = 0; i < last; ++i){
		if(books[i].bid == tmp){
			float money = 0;
			printf("请输入要修改的价格\n");
			scanf("%f", &money);
			books[i].price = money;
			showAdminMenu();
			return ;
		}
	}
	printf("查无此书\n");
	showAdminMenu();
}

void showBook() {
    int i = 0, j = 0, x = 0;
    if (user1->bid == -1) {
        for (i = 0; i < last; i++) {
            printf("编号：%d 书名：%s 作者：%s 价格：%0.2f 出版社：%s 状态：%d\n", books[i].bid, books[i].bname, books[i].author, books[i].price, books[i].publish, books[i].state);
        }
        
        showAdminMenu();

    } else {
        for (i = 0; i < last; i++) {
            if (books[i].state == 0) {
                printf("编号：%d 书名：%s 作者：%s 价格：%0.2f 出版社：%s 状态：%d\n", books[i].bid, books[i].bname, books[i].author, books[i].price, books[i].publish, books[i].state);
            }
        }
        showuserMenu();
    }
}
